package web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.yc.biz.OrderBiz;
import com.yc.biz.impl.OrderBizImpl;
import com.yc.dao.DBHelper;


@WebServlet("/orderServlet")
public class OrderServlet extends HttpServlet{
    DBHelper db=new DBHelper();
    OrderBiz ob=new OrderBizImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("收到了get请求");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");

		String op=req.getParameter("op");
		PrintWriter out=resp.getWriter();
		
		if("addCart".equals(op)){
			addCart(req,resp,out);
		}else if("getCart".equals(op)){
			getCart(req,resp,out);
		}else if("delGoods".equals(op)){
			delGoods(req,resp,out);
		}else if("changeGoodsNum".equals(op)){
			changeGoodsNum(req,resp,out);
		}else if("getAllAddr".equals(op)){
			getAllAddr(req,resp,out);
		}else if("cartGoods".equals(op)){
			cartGoods(req,resp,out);
		}else if("getInfo".equals(op)){
			getInfo(req,resp,out);
		}else if( "addAddress".equals(op) ){
			addAddress(req,resp,out);
		}else if( "setDefault".equals(op) ){
			setDefault(req,resp,out);
		}
	}

	//设置默认地址
	private void setDefault(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int mno=Integer.parseInt( req.getSession().getAttribute("mno")+"" );
		int ano=Integer.parseInt( req.getParameter("ano") );
		
		int result=ob.setDefault(mno,ano);
		if( result>0 ){
			out.print(1);
		}else{
			out.print(0);
		}
	}

	//添加地址
	private void addAddress(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int mno=Integer.parseInt( req.getSession().getAttribute("mno")+"" );
		String uname=req.getParameter("uname");
		String prov=req.getParameter("prov");
		String city=req.getParameter("city");
		String area=req.getParameter("area");
		String addr=req.getParameter("addr");
		String tel=req.getParameter("tel");
		
		int result=ob.addAddress(mno,uname,prov,city,area,addr,tel);
		if( result>0 ){
			out.print(1);
		}else{
			out.print(0);
		}
	}

	//得到用户信息
	private void getInfo(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int mno=Integer.parseInt( req.getSession().getAttribute("mno")+"" );
		
		List<Map<String, String>> list=ob.getInfo(mno);
		if(list.isEmpty()){
			out.print(0);
			return;
		}
		Gson gson = new Gson();
		out.print( gson.toJson(list) );
	}

	//得到购物车里面的商品
	private void cartGoods(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int mno=Integer.parseInt( req.getSession().getAttribute("mno")+"" );
		
		List<Map<String, String>> list=ob.cartGoods(mno);
		if(list.isEmpty()){
			out.print(0);
			return;
		}
		Gson gson = new Gson();
		out.print( gson.toJson(list) );
	}

	//得到地址
	private void getAllAddr(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int mno=Integer.parseInt( req.getSession().getAttribute("mno")+"" );
		
		List<Map<String, String>> list=ob.getAllAddr(mno);
		if(list.isEmpty()){
			out.print(0);
			return;
		}
		Gson gson = new Gson();
		out.print( gson.toJson(list) );
	}

	//改变商品数量
	private void changeGoodsNum(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int mno=Integer.parseInt( req.getSession().getAttribute("mno")+"" );
		int gno=Integer.parseInt( req.getParameter("gno") );
		int num=Integer.parseInt( req.getParameter("oldnum") );
		
		int result=ob.changeGoodsNum(mno, gno, num);
		if( result>0 ){
			out.print(1);
		}else{
			out.print(0);
		}
		
	}

	//删除商品
	private void delGoods(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int mno=Integer.parseInt( req.getSession().getAttribute("mno")+"" );
		int gno=Integer.parseInt( req.getParameter("gno") );
		
		int result=ob.delGoods(gno, mno);
		if( result>0 ){
			out.print(1);
		}else{
			out.print(0);
		}
	}

	//购物车
	private void getCart(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int mno=Integer.parseInt( req.getSession().getAttribute("mno")+"" );
		
		List<Map<String, String>> list=ob.findGoods(mno);
		if(list.isEmpty()){
			out.print(0);
			return;
		}
		Gson gson = new Gson();
		out.print( gson.toJson(list) );
	}

	//查看类别
	private void addCart(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int mno=Integer.parseInt( req.getSession().getAttribute("mno")+"" );
		int gno=Integer.parseInt(req.getParameter("gno"));
		int num=Integer.parseInt(req.getParameter("num"));
		
		int result=ob.addCart(gno, mno,num);
		if( result>0 ){
			out.print(1);
		}else{
			out.print(0);
		}
	}

		
	
}